/** ###################################################################
**     Filename  : Hmi.C
**     Project   : CyberPark
**     Processor : MC9S12XS128MAL
**     Compiler  : CodeWarrior HCS12X C Compiler
**     Date/Time : 2011-7-31, 23:02
**     Contents  :
**         User source code
**
**     (c) Copyright UNIS, a.s. 1997-2008
**     UNIS, a.s.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE Hmi */
#include "Hmi.h"
#include "Encoder.h"

HMI_PUSH_HANDLER *volatile HmipPushHandler;

U8 HmiBuzzIsOn = 1;
U8 HmiCurrentNumber;
U8 HmiIsOn;
U8 HmiNormalProcessIsRunning;

/*
void HmiBuzz(void){
    (void)HMI_BUZZ_Enable();
    HmiBuzzIsOn=1;
    Cpu_Delay100US(5000);
    (void)HMI_BUZZ_Disable(); 
    (void)HMI_BUZZ_ClrValue();
    HmiBuzzIsOn=0;  
}

void HmiBuzzOn(void){
    (void)HMI_BUZZ_Enable();
    HmiBuzzIsOn=1;    
    
}
void HmiBuzzOff(void){
    (void)HMI_BUZZ_Disable();
    HmiBuzzIsOn=0; 
    (void)HMI_BUZZ_ClrValue();
}

*/
U8 HmiByte2Segment(U8 a){
    U8 b;
    if(a==0){b=0x12;} 
    else if(a==1){b=0xfa;}
    else if(a==2){b=0x83;}
    else if(a==3){b=0xa2;}
    else if(a==4){b=0x6a;}
    else if(a==5){b=0x26;}
    else if(a==6){b=0x06;}
    else if(a==7){b=0xf2;}
    else if(a==8){b=0x02;}
    else if(a==9){b=0x22;}
    else if(a=='.'){b=0xfd;}
    return b;
}

U8 HmiByte2SegmentPoint(U8 a){
    U8 b;
    b=HmiByte2Segment(a)&HmiByte2Segment('.');
    return b;
}

void HmiInitSystem(void){
    HmiIsOn=0;
    HmiCurrentNumber=0;
    HmipPushHandler=0;
    HMI_HIGH_BYTE_PutVal(0x00);
    HMI_LOW_BYTE_PutVal(0x00);
    Cpu_Delay100US(1000);
    HMI_HIGH_BYTE_PutVal(0xff);
    HMI_LOW_BYTE_PutVal(0xff);
    return;
}

void HmiShowNumber(U16 number){
    U16 actual;
    actual=number%100;
    HMI_HIGH_BYTE_PutVal(HmiByte2Segment((U8)actual/10));
    HMI_LOW_BYTE_PutVal(HmiByte2Segment((U8)actual%10));
    return;
}

void HmiShowNumberPoint(U16 number){
    U16 actual;
    actual=number%100;
    HMI_HIGH_BYTE_PutVal(HmiByte2SegmentPoint((U8)actual/10));
    HMI_LOW_BYTE_PutVal(HmiByte2SegmentPoint((U8)actual%10));    
    return;
}

void HmiShowNumberOnePoint(U16 number){
     U16 actual;
    actual=number%100;
    HMI_HIGH_BYTE_PutVal(HmiByte2Segment((U8)actual/10));
    HMI_LOW_BYTE_PutVal(HmiByte2SegmentPoint((U8)actual%10));  
    return;   
}

void HmiIrqHandler(void){
    HMI_PUSH_HANDLER *handler;
    if(HmiIsOn){
        HmiIsOn=0;
    }else{
        HmiIsOn=1;
    }
    {
        handler=HmipPushHandler;
        if(handler){
            (*handler)();
        }
        
    }
    return;
    
}

U8 HmiWaitForDigit(void){
    U8 digit;
    HmiIsOn=1;
    HmiCurrentNumber=0;
    HmiShowNumber(HmiCurrentNumber);
    do{
        digit=HmiCurrentNumber; 
        HmiShowNumber(HmiCurrentNumber);
    }while(HmiIsOn);
    HmiShowNumberPoint(digit);
    return digit;
    
}

void HmiSpinIrqHandler(void){
    if(HmiIsOn){
        if(HMI_SPIN_DIR_GetVal()){
            HmiCurrentNumber--;
        }else{
            HmiCurrentNumber++;
        }
        if(HmiCurrentNumber>0x80){
            HmiCurrentNumber=99;
        }else if(HmiCurrentNumber>99 &&HmiCurrentNumber <0x80){
            HmiCurrentNumber=0;
        }
    }  
    return;   
}

HMI_PUSH_HANDLER *HmiSetHandler(HMI_PUSH_HANDLER *handler)
{
	HMI_PUSH_HANDLER *old = HmipPushHandler;
	HmipPushHandler = handler;
	return old;
}

void HmiNormalQuitIrqHandler(void){
    HmiNormalProcessIsRunning=0; 
    return;   
}

U16 HmiWaitForNumberEx(U16 original,U8 min,U8 max,U16 scale){
    U16 updated;
    U16 temp;
    HmiIsOn=1;
    temp=(original/scale);
    HmiCurrentNumber=(U8)(temp%100);
    do{
        HmiShowNumberOnePoint(HmiCurrentNumber);     
    }while(HmiIsOn);
    if(HmiCurrentNumber>max){
        HmiCurrentNumber=max;    
    }
    else if(HmiCurrentNumber<min){
        HmiCurrentNumber=min;    
    }
    
    updated=HmiCurrentNumber*scale;
    
    HmiShowNumberPoint(updated/scale);
    Cpu_Delay100US(2000);
    return updated;    
}

void HmiQueryPulseAccumulator(U16 *data){
    *data=EncoderValue;
    return;
}





/* END Hmi */
